package com.gupao.edu.study.thread;
/*
 * @Author: guqy
 * @Description:验证sleep 持有当前对象锁
 * @Date 15:54 2018/3/12
 * @param null
 * @return 
 */
public class ThreadTest {

    private int count = 10;
    private Object object = new Object();


    public static void main(String[] args) {
        ThreadTest threadTest = new ThreadTest();
        MyThread myThread = threadTest.new MyThread();
        MyThread myThread2 = threadTest.new MyThread();
        //启动两个线程发现的结果是，
        //在第一个线程进入sleep 之后，第二个线程没有进入sync代码区域
        //说明sleep起到了使当前线程进入了阻塞状态，并且没有释放当前object的对象锁。
        myThread.start();
        myThread2.start();
    }

    class MyThread extends Thread{
        @Override
        public void run() {
            synchronized (object){
                count++;
                System.out.println(count);
                try {
                    System.out.println("当前线程进入sleep"+Thread.currentThread().getName());
                    Thread.currentThread().sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("sleep结束"+Thread.currentThread().getName());
                count++;
                System.out.println("count"+count);
            }
        }
    }

}
